<?php
/**
 * 登录
 */
class Login extends ApiRedisBase
{
	public $validateToken = false;
	public $isLogin = false;

	public function logic() {
		$redis = $this->redis('user_info');

		if (!isset($this->params['username'])) {
			$this->returnError("empty_username");
		}
		if (!isset($this->params['password'])) {
			$this->returnError("empty_pass");
		}

		if(!$redis->exists("N:".$this->params['username'])){
			$this->returnError('no_user');
		}

		$user = $redis->hMget("N:".$this->params['username'], array('uid'));
		$uid = $user['uid'];

		$userInfo = $redis->hMget("U:$uid", array(
			'uid', 'username', 'sex', 'sex', 'nickname', 'avatar',
			'address', 'salt', 'status', 'token',
			'registerTime', 'platform', 'password', 'model',
			'mac', 'imsi', 'imei', 'ver', 'channelID'
		));

		$password = md5($this->params['password'].$userInfo['salt']);
		if ($password != $userInfo['password']){
			$this->returnError('fault_pass');
		}

		if (1 != $userInfo['status']){
			$this->returnError('is_stopped_account');
		}

		$userInfo['serverID'] = $this->redis('status')->hGet('uid2serverID', $uid);

		if ( ! $roomID = (int) $this->redis('status')->hget('uid2roomID', $uid)) {
			$roomID = (int) $this->redis('room_status')->hMget('UR:' . $uid, ['roomID'])['roomID'];
		}

		$userInfo['roomID'] = $roomID;

		//更新登陆时间和token值
		$now = time();
		$token = md5($now . $userInfo['salt']);
		$redis->hMset("U:$uid", array("lastLoginTime" => $now, "token" => $token));

		unset(
			$userInfo['imei'],$userInfo['imsi'],$userInfo['mac'],
			$userInfo['model'],$userInfo['salt'],$userInfo['password']
		);

		$this->returnData('suc_login', $userInfo);
	}
}
?>